package com.ayoka.parenting.ui.meditation.question;

import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;

import com.ayoka.parenting.R;
import com.ayoka.parenting.adapter.MeditationQuestionOptionAdapter;
import com.ayoka.parenting.bean.MeditationDetailBean;
import com.ayoka.parenting.bean.MeditationQuestionBean;
import com.ayoka.parenting.databinding.FragmentMeditationQuestionDetailBinding;
import com.ayoka.parenting.databinding.FragmentMeditationQuestionStartBinding;
import com.ayoka.parenting.interf.SimpleTextWatchListener;
import com.ayoka.parenting.mvp.BasePresenter;
import com.ayoka.parenting.mvp.BaseView;
import com.ayoka.parenting.mvp.MVPBaseFragment;
import com.jincp.common.brvah.BaseQuickAdapter;
import com.jincp.common.brvah.layoutmanager.MyLinearLayoutManager;
import com.jincp.common.function.FunctionHasParamNoResult;
import com.jincp.common.function.FunctionManager;

import java.util.List;

/**
 * @ProjectName: MVPPlugin.
 * @ClassName: MeditationActivity.
 * @Description: .
 * @Author: jincp.
 * @CreateDate: 2019/10/10 18:17.
 */
public class MeditationQuestionDetailFragment extends MVPBaseFragment<FragmentMeditationQuestionDetailBinding, BaseView, BasePresenter> {

    public static MeditationQuestionDetailFragment newInstance(MeditationQuestionBean meditationQuestionBean, boolean isFirst, boolean isLast) {
        MeditationQuestionDetailFragment fragment = new MeditationQuestionDetailFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable("MeditationQuestionBean", meditationQuestionBean);
        bundle.putBoolean("IsFirst", isFirst);
        bundle.putBoolean("IsLast", isLast);
        fragment.setArguments(bundle);
        return fragment;
    }

    private MeditationQuestionBean meditationQuestionBean;

    private boolean isFirst, isLast;

    @Override
    protected void configBundleArguments() {
        Bundle bundle = getArguments();
        if (bundle != null) {
            meditationQuestionBean = (MeditationQuestionBean) bundle.getSerializable("MeditationQuestionBean");
            isFirst = bundle.getBoolean("IsFirst");
            isLast = bundle.getBoolean("IsLast");
        }
    }


    @Override
    protected int getContentLayout() {
        return R.layout.fragment_meditation_question_detail;
    }

    @Override
    protected void initViews(View view) {
        if (isFirst) {
            mDataBinding.includeMeditationQuestionOperation.layoutPrevious.setEnabled(false);
            mDataBinding.includeMeditationQuestionOperation.tvPrevious.setEnabled(false);
            mDataBinding.includeMeditationQuestionOperation.ivPrevious.setColorFilter(Color.LTGRAY);
        } else {
            mDataBinding.includeMeditationQuestionOperation.layoutPrevious.setEnabled(true);
            mDataBinding.includeMeditationQuestionOperation.tvPrevious.setEnabled(true);
            mDataBinding.includeMeditationQuestionOperation.ivPrevious.setColorFilter(Color.LTGRAY);
        }
        if (isLast) {
            mDataBinding.includeMeditationQuestionOperation.tvNext.setText("保存提交");
        }
        mDataBinding.includeMeditationQuestionOperation.layoutNext.setSelected(true);
        if (meditationQuestionBean != null && meditationQuestionBean.Answer != null && meditationQuestionBean.Answer.AnswerOption != null) {
            configNextContainer(!meditationQuestionBean.Answer.AnswerOption.isEmpty());
        } else {
            configNextContainer(false);
        }

        mDataBinding.includeMeditationQuestionOperation.layoutNext.setEnabled(false);
        mDataBinding.includeMeditationQuestionOperation.tvNext.setEnabled(false);
        mDataBinding.includeMeditationQuestionOperation.ivNext.setColorFilter(Color.LTGRAY);

        mDataBinding.includeMeditationQuestionOperation.layoutPrevious.setOnClickListener(this);
        mDataBinding.includeMeditationQuestionOperation.layoutNext.setOnClickListener(this);
    }

    private void configNextContainer(boolean enable) {
        mDataBinding.includeMeditationQuestionOperation.layoutNext.setEnabled(enable);
        mDataBinding.includeMeditationQuestionOperation.tvNext.setEnabled(enable);
        if (enable) {
            mDataBinding.includeMeditationQuestionOperation.ivNext.clearColorFilter();
        } else {
            mDataBinding.includeMeditationQuestionOperation.ivNext.setColorFilter(Color.LTGRAY);
        }
    }

    @Override
    protected void initData() {
        if (meditationQuestionBean != null) {
            mDataBinding.tvQuestion.setText(meditationQuestionBean.Question);
            if (meditationQuestionBean.QuestionType == 0 || meditationQuestionBean.QuestionType == 1) {
                mDataBinding.recyclerViewQuestionOption.setVisibility(View.VISIBLE);
                if (meditationQuestionBean.Options != null) {
                    MeditationQuestionOptionAdapter adapter = new MeditationQuestionOptionAdapter(meditationQuestionBean.QuestionType);
                    adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                        @Override
                        public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                            if (meditationQuestionBean.QuestionType == MeditationQuestionOptionAdapter.SINGLE_MODE) {
                                ((MeditationQuestionOptionAdapter) adapter).setSingleChoice(position);
                            } else if (meditationQuestionBean.QuestionType == MeditationQuestionOptionAdapter.MULTI_MODE) {
                                ((MeditationQuestionOptionAdapter) adapter).setMultiChoice(position);
                            }
                            List result = ((MeditationQuestionOptionAdapter) adapter).getChoiceData();
                            configNextContainer(!result.isEmpty());
                            if (meditationQuestionBean != null && meditationQuestionBean.Answer != null) {
                                meditationQuestionBean.Answer.AnswerOption = result;
                            }
                        }
                    });
                    adapter.setNewData(meditationQuestionBean.Options);
                    mDataBinding.recyclerViewQuestionOption.setLayoutManager(new MyLinearLayoutManager(getActivity()));
                    mDataBinding.recyclerViewQuestionOption.setAdapter(adapter);
                }
            } else if (meditationQuestionBean.QuestionType == 2 ) {
                mDataBinding.edtContent.setVisibility(View.VISIBLE);
                mDataBinding.edtContent.addTextChangedListener(new SimpleTextWatchListener(){
                    @Override
                    public void afterTextChanged(Editable s) {
                        configNextContainer(s.length() != 0);
                        if (meditationQuestionBean != null && meditationQuestionBean.Answer != null) {
                            meditationQuestionBean.Answer.Answer = s.toString().trim();
                        }
                    }
                });
            }
        }
    }

    @Override
    protected BasePresenter getPresenter() {
        return null;
    }

    @Override
    public void onClick(View view) {
        if (view == mDataBinding.includeMeditationQuestionOperation.layoutPrevious) {
            FunctionManager.getInstance().invokeFunction("QuestionPrevious");
        } else if (view == mDataBinding.includeMeditationQuestionOperation.layoutNext) {
            FunctionManager.getInstance().invokeFunction("QuestionNext");
        }
    }
}
